/*
-----------------------------------------------------------------------------
This source file is part of Labor3D
(Labor 3D Graphics Engine)

Copyright (c) 2011-2020 Rock Mao
* creator : Rock Mao
* created : 2011-10-25   22:29
* filename: LaborD3D11RrenderState.cpp
-----------------------------------------------------------------------------
*/
#include "stdafx.h"
#include "LaborD3D11RrenderState.h"
#include "LaborCore.h"
#include "LaborD3D11RenderSystem.h"
NAMESPACE_LABOR3D_BEGIN
//---------------------------------------------------------------------
CLD3D11SamplerState::CLD3D11SamplerState()
{
	m_pD3D11Sampler = NULL;

}

//---------------------------------------------------------------------
CLD3D11SamplerState::~CLD3D11SamplerState()
{
	_destroy();
}

//---------------------------------------------------------------------
bool CLD3D11SamplerState::load(const wchar_t* fileName , unsigned long  arg)
{
	return load(fileName, NULL, 0, 0 );
}

//---------------------------------------------------------------------
bool CLD3D11SamplerState::load(const wchar_t* fileName , const unsigned char* buf , size_t bufLen, unsigned long arg)
{
	HRESULT hr = S_OK;
	D3D11_SAMPLER_DESC sample_desc;
	sample_desc.Filter        = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
	sample_desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
	sample_desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
	sample_desc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
	sample_desc.MinLOD   = -FLT_MAX;
	sample_desc.MaxLOD   = FLT_MAX;
	sample_desc.MipLODBias = 0;
	sample_desc.MaxAnisotropy = 1;

	ID3D11Device* _pd3dDevice = (ID3D11Device*)(GetRenderSystem()->getDevice());
	hr = _pd3dDevice->CreateSamplerState( &sample_desc, &m_pD3D11Sampler );

	return hr == S_OK;
}

//---------------------------------------------------------------------
bool CLD3D11SamplerState::create( const sLSamplerStateDesc & _desc )
{
	HRESULT hr = S_OK;
	D3D11_SAMPLER_DESC sample_desc;
	sample_desc.Filter                = (D3D11_FILTER)_desc.m_eFilter;
	sample_desc.AddressU         = (D3D11_TEXTURE_ADDRESS_MODE)_desc.m_eAddressU;
	sample_desc.AddressV         = (D3D11_TEXTURE_ADDRESS_MODE)_desc.m_eAddressV;
	sample_desc.AddressW        = (D3D11_TEXTURE_ADDRESS_MODE)_desc.m_eAddressW;
	sample_desc.MinLOD           = _desc.m_fMinLOD;
	sample_desc.MaxLOD          = _desc.m_fMaxLOD;
	sample_desc.MipLODBias     = _desc.m_fMipLODBias;
	sample_desc.MaxAnisotropy = _desc.m_nMaxAnisotropy;

	ID3D11Device* _pd3dDevice = (ID3D11Device*)(GetRenderSystem()->getDevice());
	hr = _pd3dDevice->CreateSamplerState( &sample_desc, &m_pD3D11Sampler );

	return hr == S_OK;
}

//---------------------------------------------------------------------
bool CLD3D11SamplerState::isLoaded()
{
	return m_pD3D11Sampler != NULL;
}

//---------------------------------------------------------------------
bool CLD3D11SamplerState::unload()
{
	_destroy();
	return true;
}

//---------------------------------------------------------------------
unsigned long CLD3D11SamplerState::memUsage()
{
	return 1;
}

//---------------------------------------------------------------------
void CLD3D11SamplerState::_destroy()
{
	if ( m_pD3D11Sampler )
	{
		m_pD3D11Sampler->Release();
		m_pD3D11Sampler = NULL;
	}
	
}
//---------------------------------------------------------------------
NAMESPACE_LABOR3D_END